package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import com.android.kotlincoroutinemvvmdemo.R

/*
* 箭头刷新view
* */
class DoubleArrowRefreshView2 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val degreeArr = floatArrayOf(0F, 180F)

    private var mSpace = context.resources.getDimension(R.dimen.dp_5)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeWidth = context.resources.getDimension(R.dimen.dp_2)
            color = context.resources.getColor(R.color.color_B128C9)
            style = Paint.Style.STROKE
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_B128C9)
            style = Paint.Style.FILL
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth / 2.0F
            val cy = mHeight / 2.0F
            translate(cx, cy)
            for (degree in degreeArr) {
                save()
                rotate(60F + degree)
                mPath.apply {
                    moveTo(cx - 2 * mSpace, 0F)
                    lineTo(cx - mSpace, mSpace)
                    lineTo(cx, 0F)
                    close()
                }
                drawPath(mPath, mPaint1)
                drawArc(
                    -cx + mSpace,
                    -cy + mSpace,
                    cx - mSpace,
                    cy - mSpace,
                    210F,
                    150F,
                    false,
                    mPaint0
                )
                restore()
            }
        }
    }
}